<template>
  <div class="edit-container">
    <!-- 顶部栏 -->
    <van-nav-bar
      title="编辑资料"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <!-- 头像 -->
    <div class="photo">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息输入框 -->
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="editNameClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="birthdayClick"
      />
    </van-cell-group>
    <!-- 姓名输入框 -->
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="updatesName"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入姓名" />
    </van-dialog>
    <!-- 性别输入框 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="编辑性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="updatesgender(0)" />
      <van-cell title="女" is-link @click="updatesgender(1)" />
    </van-popup>
    <!-- 生日输入框 -->
    <van-popup v-model="showDateTime" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDateTime = false"
        @confirm="updatesBirthday"
      />
    </van-popup>
    <!-- 图片裁切 -->
    <div class="cropper" v-show="shouCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
        centerBox
      ></vueCropper>
      <van-button type="info" @click="updatePhoto">确认</van-button>
      <van-button type="danger" @click="shouCropper = false"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入vuex辅助函数
import { mapState } from 'vuex'

// 导入时间插件
import * as moment from 'moment'

// 导入编辑用户资料接口
import { editUserInfo } from './user.js'

// 导入编辑用户头像接口
import { editUserPhoto } from './user.js'

// 图片裁切
import { VueCropper } from 'vue-cropper'

export default {
  name: 'edit',
  data () {
    return {
      showDialog: false,
      newName: '',
      showPopup: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2030, 12, 30),
      currentDate: new Date(),
      showDateTime: false,
      img: '',
      shouCropper: false
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    // 姓名点击事件
    editNameClick () {
      this.showDialog = true
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    // 生日点击事件
    birthdayClick () {
      this.showDateTime = true
      this.currentDate = new Date(this.userInfo.birthday)
    },
    // 修改姓名
    updatesName () {
      editUserInfo({ name: this.newName }).then(res => {
        this.$store.commit('update', {
          ...this.userInfo,
          name: this.newName
        })
        this.$toast.success('修改成功')
      })
    },
    // 修改性别
    updatesgender (data) {
      editUserInfo({gender:data}).then(res => {
        this.$store.commit('update', {
          ...this.userInfo,
          gender: data
        })
        this.$toast.success('修改成功')
        this.showPopup = false
        this.$toast.success('修改成功')
      })
    },
    // 修改生日
    updatesBirthday () {
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        this.showDateTime = false
        this.$store.commit('update', {
          ...this.userInfo,
          birthday
        })
        this.$toast.success('修改成功')
      })
    },
    // 图片裁切
    afterRead (file) {
      this.shouCropper = true
      this.img = file.content
    },
    // 修改头像
    updatePhoto () {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(async data => {
        let fd = new FormData()
        fd.append('photo', data)
        let res = await editUserPhoto(fd)
          this.$store.commit('update', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.$toast.success('修改成功');
          this.shouCropper = false;
      })
    }
  },
  components: {
    VueCropper
  }
}
</script>

<style lang="less">
.edit-container {
  .van-nav-bar__content {
    background-color: #0098fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-nav-bar__arrow {
      color: #fff;
    }
    .van-nav-bar__text {
      color: #fff;
    }
  }
  .photo {
    display: flex;
    justify-content: center;
    padding: 25px 0;
  }
  .van-uploader {
    position: absolute;
    opacity: 0;
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 130px;
      height: 120px;
    }
  }
  .cropper {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
    .van-button--danger {
      position: absolute;
      left: 0;
      bottom: 0;
    }
  }
}
</style>
